
var initData = function(num){
	var url = base+"/projectCenter/designcell/getConstantListPage";
	//debugger
	var param = {
			page_num:num,
			page_size:"10",
            project_id:project_id,
            design_cell_id:design_cell_id,
            constant_table_code:constant_table_code
	};
	$.ajax({
        url: url,
        data: param,
        success: function (data, textStatus, jqXHR) {
        	//debugger
        	if(data != null){
	        	var pageCount = data.dataList.pager.pageCount;
	        	var dataObj ={
	        			"list":data.dataList.list,
	         			"dataTool":{
	         				"title":{
                               // "id":"id",
                                "constant_table_code":"常量集编号",
                                "code_name": "常量代号",
                                "code_cname":"常量中文名",
                                "code_parent_name":"上级常量代号",
                                "has_children":"是否包含子集",
                                "code_value":"常量取值",
                                "code_value_unit":"常量取值单位",
                                "code_default_value":"常量默认值",
                            	"operation":"操作"
	            			},
	            			"titleCheckbox":false,
							"btn":[{"event":"updateObj","name":"编辑"}]
						}
		  		};
	        	showPageTable('view', dataObj,num,pageCount,"initData");
        	}
        },
        error: function () {
        }
    });
	
}

//编辑常量值
var updateObj = function(obj){
    var contentDIV = function(){
        var content;
        var data = {
            "title":{
                "id":"id",
                "version_id":"version_id",
                "standard_type":"standard_type",
                "code_parent_name":"code_parent_name",
                "constant_table_code":"constant_table_code",
                "code_name":"数据项代号",
                "code_cname":"数据项中文简称",
                "code_value":"数据项取值",
                "has_children":"是否包含代码集",
                "code_value_unit":"数据项取值单位",
                "code_default_value":"数据项默认取值",
                "remark":"备注"
            },"text":{
                "id":obj.id,
                "version_id":obj.version_id,
                "standard_type":obj.standard_type,
                "code_parent_name":obj.code_parent_name,
                "constant_table_code":obj.constant_table_code,
                "code_name":obj.code_name,
                "code_cname":obj.code_cname,
                "code_value":obj.code_value,
                "has_children":"",
                "code_value_unit":obj.code_value_unit,
                "code_default_value":obj.code_default_value,
                "remark":obj.remark
            },"input":{
                "id":"hidden",
                "version_id":"hidden",
                "standard_type":"hidden",
                "code_parent_name":"hidden",
                "constant_table_code":"hidden",
                "code_name":"text",
                "code_cname":"text",
                "code_value":"text",
                "has_children":"switch",
                "code_value_unit":"text",
                "code_default_value":"text",
                "remark":"text"
            },"input_init":{
            }
        };
        var getDiv = dc_update.innerHTML;
        laytpl(getDiv).render(data,function(html){
            content = html;
        });
        return content;
    }
    layopen(contentDIV());
    initSwitch(obj.has_children);
}

var initSwitch = function(flag){
    var html = '';
    if(flag=="true" || flag=="1"){
        html += '<input checked="" name="has_children" lay-skin="switch" lay-filter="switch" lay-text="是|否" type="checkbox" > ';
    }else{
        html += '<input name="has_children" lay-skin="switch" lay-filter="switch" lay-text="是|否" type="checkbox" > ';
    }
    $("#switch").html(html);
    form.render();
}

//数据校验
var verifyData = function(data){
    if(data.code_name == ''){
        var obj = $("input[name='code_name']");
        obj.focus();
        layer.tips('请填写此字段!', obj);
        return false;
    }
    if(data.code_cname == ''){
        var obj = $("input[name='code_cname']");
        obj.focus();
        layer.tips('请填写此字段!', obj);
        return false;
    }
    if(data.code_value == ''){
        var obj = $("input[name='code_value']");
        obj.focus();
        layer.tips('请填写此字段!', obj);
        return false;
    }
    if(data.code_value_unit == ''){
        var obj = $("input[name='code_value_unit']");
        obj.focus();
        layer.tips('请填写此字段!', obj);
        return false;
    }
    if(data.code_default_value == ''){
        var obj = $("input[name='code_default_value']");
        obj.focus();
        layer.tips('请填写此字段!', obj);
        return false;
    }
    if(data.code_parent_name == ''){
        alert("上级数据项选项不能为空!");
        return false;
    }
    return true;
}

var submitData = function(data){
    if(data.has_children == "on"){
        data.has_children = true;
    }else{
        data.has_children = false;
    }
    var cpn = "";
    if(data.code_parent_name=="null") {
        cpn = "";
    }else{
        cpn = data.code_parent_name;
    }
    var param = {
        id:data.id,
        project_id:project_id,
        version_id:data.version_id,
        standard_type:data.standard_type,
        code_parent_name:cpn,
        constant_table_code:data.constant_table_code,
        code_name:data.code_name,
        code_cname:data.code_cname,
        code_parent_name:data.code_parent_name,
        has_children:data.has_children,
        code_value:data.code_value,
        code_value_unit:data.code_value_unit,
        code_default_value:data.code_default_value,
        remark:data.remark
    }
    $.ajax({
        url:base+"/projectCenter/designcell/updateConstant",
        data:param,
        success:function(data,status){
            if(data.statusCode == 0){
                layer.msg(data.message, {
                    icon : 1
                });
            }else{
                layer.msg(data.message, {
                    icon : 5
                });
            }
            initData(1);
        }
    });
}

//渲染弹窗
var layopen = function(contentDiv){
    layer.open({
        title:'更新常量'
        ,content: contentDiv
        ,area: ['700px', '700px']
        ,scrollbar: false
        ,btn: ['确认', '取消']
        ,yes: function(index,layero){ //确认
            var data = $(layero).find("form").serializeJSON();
            if(verifyData(data)){
                submitData(data);
            }
        }
    });
}


var back=function(){
	 var url = base+"/projectCenter/designcell/index/"+design_cell_id+"/"+project_id+"/"+data_id;
     $.get(url, function(result){
  	   $(".layui-show").html("");
  	   $(".layui-show").html(result);
     });
}
var showButton = function(){
    var button_html = ' <div class="btn-team">'+
        ' <button class="btn btn-default" id="back" onclick="back()">返回</button>';
    $(".btn-team").remove();
    $("#div_content").prepend(button_html);
}

var data_id = $.trim($("#data_id").val());
var constant_table_code = $("#constant_table_code").val();

var init_select = function(){
	$("[data_id='"+data_id+"']").removeClass(".layui-this").addClass(".layui-this");
}

$(document).ready(function(){
    showButton();
    init_select();
	initData(1);
});